/**
 * Created by JiaJia on 2019/5/23 0023.
 * 链表中环的入口
 * 快慢指针的思想
 * 快指针走了2x,慢指针走了x,两者在环中相遇，而此时快指针刚好比慢指针多走了一个环的距离
 */
public class Code56 {
    public class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode EntryNodeOfLoop(ListNode pHead)
    {
        if (pHead==null||pHead.next==null){
            return null;
        }
        ListNode slow=pHead;
        ListNode fast=pHead;
        while (fast!=null&&fast.next!=null){
            slow=slow.next;
            fast=fast.next.next;
            if (slow==fast){
                fast=pHead;
                while (fast!=slow){
                    slow=slow.next;
                    fast=fast.next;
                }
                if (fast==slow){
                    return slow;
                }
            }

        }return null;
    }
}